Completed
Push — develop ( 4315f1...9a4836 )
by Xaver
29s
created

map.js ➔ ... ➔ document.forEach   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
dl 0
loc 4
rs 10
nop 1
1
define(['map/clientlayer', 'map/labellayer', 'map/button', 'leaflet'],
2
  function (ClientLayer, LabelLayer, Button, L) {
3
    'use strict';
4
5
    var options = {
6
      worldCopyJump: true,
7
      zoomControl: true,
8
      minZoom: 0
9
    };
10
11
    return function (config, linkScale, sidebar, router, buttons) {
12
      var self = this;
13
      var savedView;
14
15
      var map;
16
      var layerControl;
17
      var baseLayers = {};
18
19
      function saveView() {
20
        savedView = {
21
          center: map.getCenter(),
22
          zoom: map.getZoom()
23
        };
24
      }
25
26
      function contextMenuOpenLayerMenu() {
27
        document.querySelector('.leaflet-control-layers').classList.add('leaflet-control-layers-expanded');
28
      }
29
30
      var el = document.createElement('div');
31
      el.classList.add('map');
32
33
      map = L.map(el, options);
34
      var now = new Date();
35
      config.mapLayers.forEach(function (item, i) {
36
        if ((typeof item.config.start === 'number' && item.config.start <= now.getHours()) || (typeof item.config.end === 'number' && item.config.end > now.getHours())) {
37
          item.config.order = item.config.start * -1;
38
        } else {
39
          item.config.order = i;
40
        }
41
      });
42
43
      config.mapLayers = config.mapLayers.sort(function (a, b) {
44
        return a.config.order - b.config.order;
45
      });
46
47
      var layers = config.mapLayers.map(function (d) {
48
        return {
49
          'name': d.name,
50
          'layer': L.tileLayer(d.url.replace('{retina}', L.Browser.retina ? '@2x' : ''), d.config)
51
        };
52
      });
53
54
      map.addLayer(layers[0].layer);
55
56
      layers.forEach(function (d) {
57
        baseLayers[d.name] = d.layer;
58
      });
59
60
      var button = new Button(config, map, router, buttons);
61
62
      map.on('locationfound', button.locationFound);
63
      map.on('locationerror', button.locationError);
64
      map.on('dragend', saveView);
65
      map.on('contextmenu', contextMenuOpenLayerMenu);
66
67
      button.init();
68
69
      layerControl = L.control.layers(baseLayers, [], { position: 'bottomright' });
70
      layerControl.addTo(map);
71
72
      map.zoomControl.setPosition('topright');
73
74
      var clientLayer = new ClientLayer({ minZoom: config.clientZoom });
75
      clientLayer.addTo(map);
76
      clientLayer.setZIndex(5);
77
78
      var labelLayer = new LabelLayer({ minZoom: config.labelZoom });
79
      labelLayer.addTo(map);
80
      labelLayer.setZIndex(6);
81
82
      map.on('zoom', function () {
83
        clientLayer.redraw();
84
        labelLayer.redraw();
85
      });
86
87
      map.on('baselayerchange', function (e) {
88
        map.options.maxZoom = e.layer.options.maxZoom;
89
        clientLayer.options.maxZoom = map.options.maxZoom;
90
        labelLayer.options.maxZoom = map.options.maxZoom;
91
        if (map.getZoom() > map.options.maxZoom) {
92
          map.setZoom(map.options.maxZoom);
93
        }
94
95
        var style = document.querySelector('.css-mode:not([media="not"])');
96
        if (style && e.layer.options.mode !== '' && !style.classList.contains(e.layer.options.mode)) {
97
          style.media = 'not';
98
          labelLayer.updateLayer();
99
        }
100
        if (e.layer.options.mode) {
101
          var newStyle = document.querySelector('.css-mode.' + e.layer.options.mode);
102
          newStyle.media = '';
103
          newStyle.appendChild(document.createTextNode(''));
104
          labelLayer.updateLayer();
105
        }
106
      });
107
108
      map.on('load', function () {
109
        var inputs = document.querySelectorAll('.leaflet-control-layers-selector');
110
        [].forEach.call(inputs, function (input) {
111
          input.setAttribute('role', 'radiogroup');
112
          input.setAttribute('aria-label', input.nextSibling.innerHTML.trim());
113
        });
114
      });
115
116
      var nodeDict = {};
117
      var linkDict = {};
118
      var highlight;
119
120
      function resetMarkerStyles(nodes, links) {
121
        Object.keys(nodes).forEach(function (d) {
122
          nodes[d].resetStyle();
123
        });
124
125
        Object.keys(links).forEach(function (d) {
126
          links[d].resetStyle();
127
        });
128
      }
129
130
      function setView(bounds, zoom) {
131
        map.fitBounds(bounds, { paddingTopLeft: [sidebar(), 0], maxZoom: (zoom ? zoom : config.nodeZoom) });
132
      }
133
134
      function goto(m) {
135
        var bounds;
136
137
        if ('getBounds' in m) {
138
          bounds = m.getBounds();
139
        } else {
140
          bounds = L.latLngBounds([m.getLatLng()]);
141
        }
142
143
        setView(bounds);
144
145
        return m;
146
      }
147
148
      function updateView(nopanzoom) {
149
        resetMarkerStyles(nodeDict, linkDict);
150
        var m;
151
152
        if (highlight !== undefined) {
153
          if (highlight.type === 'node' && nodeDict[highlight.o.node_id]) {
154
            m = nodeDict[highlight.o.node_id];
155
            m.setStyle({ color: '#ad2358', weight: 8, fillOpacity: 1, opacity: 0.4, className: 'stroke-first' });
156
          } else if (highlight.type === 'link' && linkDict[highlight.o.id]) {
157
            m = linkDict[highlight.o.id];
158
            m.setStyle({ weight: 4, opacity: 1, dashArray: '5, 10' });
159
          }
160
        }
161
162
        if (!nopanzoom) {
163
          if (m) {
164
            goto(m);
165
          } else if (savedView) {
166
            map.setView(savedView.center, savedView.zoom);
167
          } else {
168
            setView(config.fixedCenter);
169
          }
170
        }
171
      }
172
173
      self.setData = function setData(data) {
174
        nodeDict = {};
175
        linkDict = {};
176
177
        clientLayer.setData(data);
178
        labelLayer.setData(data, map, nodeDict, linkDict, linkScale, router, config);
179
180
        updateView(true);
181
      };
182
183
      self.resetView = function resetView() {
184
        button.disableTracking();
185
        highlight = undefined;
186
        updateView();
187
      };
188
189
      self.gotoNode = function gotoNode(d) {
190
        button.disableTracking();
191
        highlight = { type: 'node', o: d };
192
        updateView();
193
      };
194
195
      self.gotoLink = function gotoLink(d) {
196
        button.disableTracking();
197
        highlight = { type: 'link', o: d };
198
        updateView();
199
      };
200
201
      self.gotoLocation = function gotoLocation(d) {
202
        button.disableTracking();
203
        map.setView([d.lat, d.lng], d.zoom);
204
      };
205
206
      self.destroy = function destroy() {
207
        button.clearButtons();
208
        map.remove();
209
210
        if (el.parentNode) {
211
          el.parentNode.removeChild(el);
212
        }
213
      };
214
215
      self.render = function render(d) {
216
        d.appendChild(el);
217
        map.invalidateSize();
218
      };
219
220
      return self;
221
    };
222
  });
223